package com.wangsh.forward.system.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wangsh.forward.system.dao.IASysHistoryDao;
import com.wangsh.forward.system.dao.IASysPropertyDao;
import com.wangsh.forward.system.dao.impl.ASysHistoryDaoImpl;
import com.wangsh.forward.system.dao.impl.ASysPropertyDaoImpl;
import com.wangsh.forward.system.pojo.ASysHistory;
import com.wangsh.forward.system.pojo.ASysProperty;
import com.wangsh.forward.system.service.ISystemService;
import com.wangsh.webforward.common.pojo.ApiResponse;
import com.wangsh.webforward.common.pojo.ApiResponseEnum;
import com.wangsh.webforward.common.pojo.JSONEnum;
import com.wangsh.webforward.common.service.BaseServiceImpl;
import com.wangsh.webforward.common.util.ConstatFinalUtil;
import com.wangsh.webforward.common.util.PageInfoUtil;

/**
 * System模块Service的实现类
 * @author TeaBig
 */
public class SystemServiceImpl extends BaseServiceImpl implements ISystemService
{
	/* 系统配置Dao */
	private IASysPropertyDao sypPropertyDao = new ASysPropertyDaoImpl();
	private IASysHistoryDao sysHistoryDao = new ASysHistoryDaoImpl();
	@Override
	public ApiResponse<Object> saveOnePropertyService(ASysProperty sysProperty)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 按照名字查询 */
		condMap.clear();
		condMap.put("name", sysProperty.getName());
		ApiResponse<ASysProperty> apiResponseRes = this.findOnePropertyService(condMap);
		ASysProperty sysPropertyRes = apiResponseRes.getDataOneJava();
		if(sysPropertyRes != null)
		{
			/* 说明记录已经存在, */
			apiResponse.setCode(ApiResponseEnum.INFO_RECORD_EXISTS.getStatus());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
			return apiResponse ; 
		}
		
		sysProperty.setCreateTime(new Date());
		sysProperty.setUpdateTime(new Date());
		int res = this.sypPropertyDao.save(sysProperty);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), sysProperty.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), sysProperty);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOnePropertyService(ASysProperty sysProperty)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		sysProperty.setUpdateTime(new Date());
		int res = this.sypPropertyDao.update(sysProperty.getId(),sysProperty);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), sysProperty.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), sysProperty);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOnePropertyService(Map<String, Object> condMap)
	{
		return null;
	}

	@Override
	public ApiResponse<ASysProperty> findCondListPropertyService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<ASysProperty> apiResponse = new ApiResponse<ASysProperty>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<ASysProperty> SysProList = Collections.EMPTY_LIST;

		Map<String, List<ASysProperty>> dataList = new HashMap<String, List<ASysProperty>>();
		try
		{
			/* 对关键字进行模糊匹配 */
			if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
			{
				condMap.put("keyword", "%" + condMap.get("keyword") + "%");
			}

			/* 设置分页相关信息 */
			if (pageInfoUtil != null)
			{
				/* 设置分页信息 */
				SysProList = this.sypPropertyDao.findList(condMap);
				/* 向接口中放分页信息 */
				apiResponse.setPageInfoUtil(pageInfoUtil);
			} else
			{
				SysProList = this.sypPropertyDao.findList(condMap);
			}
			/* 将结果放到JSON中 */
			dataList.put(ApiResponseEnum.NAME_LIST.getName(), SysProList);

			apiResponse.setDataListJava(SysProList);
			apiResponse.setDataList(dataList);
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
					Collections.EMPTY_MAP);
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("查询多条记录失败了", e);
			apiResponse.setCode(ApiResponseEnum.STATUS_SERVER_ERROR.getStatus());
			Map<String, String> infoMap = new HashMap<String, String>();
			/* 报错信息的占位符 */
			infoMap.put("errinfo", e.toString());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), infoMap);
		}
		return apiResponse;
	}

	@Override
	public ApiResponse<ASysProperty> findOnePropertyService(Map<String, Object> condMap)
	{
		ApiResponse<ASysProperty> apiResponse = new ApiResponse<ASysProperty>();

		Map<String, ASysProperty> data = new HashMap<String, ASysProperty>();
		try
		{
			ASysProperty SysPro = this.sypPropertyDao.findOne(condMap);
			data.put(ApiResponseEnum.NAME_ONE.getName(), SysPro);
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
			/* 将结果放到JSON中 */
			apiResponse.setDataOne(data);
			apiResponse.setDataOneJava(SysPro);
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
					Collections.EMPTY_MAP);
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("查询一条记录失败了", e);
			apiResponse.setCode(ApiResponseEnum.STATUS_SERVER_ERROR.getStatus());
			Map<String, String> infoMap = new HashMap<String, String>();
			/* 报错信息的占位符 */
			infoMap.put("errinfo", e.toString());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), infoMap);
		}
		return apiResponse;
	}
	
	
	@Override
	public ApiResponse<Object> saveOneHistoryService(ASysHistory sysHistory) {
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		sysHistory.setCreateTime(new Date());
		sysHistory.setPubTime(new Date());
		sysHistory.setUpdateTime(new Date());
		int res = this.sysHistoryDao.save(sysHistory);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), sysHistory.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), sysHistory);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneHistoryService(ASysHistory sysHistory) {
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		sysHistory.setUpdateTime(new Date());
		int res = this.sysHistoryDao.update(sysHistory.getId(),sysHistory);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), sysHistory.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), sysHistory);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneHistoryService(Map<String, Object> condMap) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ApiResponse<ASysHistory> findCondListHistoryService(PageInfoUtil pageInfoUtil,
			Map<String, Object> condMap) {
		ApiResponse<ASysHistory> apiResponse = new ApiResponse<ASysHistory>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<ASysHistory> SysHisList = Collections.EMPTY_LIST;

		Map<String, List<ASysHistory>> dataList = new HashMap<String, List<ASysHistory>>();
		try
		{
			/* 对关键字进行模糊匹配 */
			if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
			{
				condMap.put("keyword", "%" + condMap.get("keyword") + "%");
			}

			/* 设置分页相关信息 */
			if (pageInfoUtil != null)
			{
				/* 设置分页信息 */
				SysHisList = this.sysHistoryDao.findList(condMap);
				/* 向接口中放分页信息 */
				apiResponse.setPageInfoUtil(pageInfoUtil);
			} else
			{
				SysHisList = this.sysHistoryDao.findList(condMap);
			}
			/* 将结果放到JSON中 */
			dataList.put(ApiResponseEnum.NAME_LIST.getName(), SysHisList);

			apiResponse.setDataListJava(SysHisList);
			apiResponse.setDataList(dataList);
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
					Collections.EMPTY_MAP);
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("查询多条记录失败了", e);
			apiResponse.setCode(ApiResponseEnum.STATUS_SERVER_ERROR.getStatus());
			Map<String, String> infoMap = new HashMap<String, String>();
			/* 报错信息的占位符 */
			infoMap.put("errinfo", e.toString());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), infoMap);
		}
		return apiResponse;
	}

	@Override
	public ApiResponse<ASysHistory> findOneHistoryService(Map<String, Object> condMap) {
		ApiResponse<ASysHistory> apiResponse = new ApiResponse<ASysHistory>();

		Map<String, ASysHistory> data = new HashMap<String, ASysHistory>();
		try
		{
			ASysHistory Syshis = this.sysHistoryDao.findOne(condMap);
			data.put(ApiResponseEnum.NAME_ONE.getName(), Syshis);
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
			/* 将结果放到JSON中 */
			apiResponse.setDataOne(data);
			apiResponse.setDataOneJava(Syshis);
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
					Collections.EMPTY_MAP);
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("查询一条记录失败了", e);
			apiResponse.setCode(ApiResponseEnum.STATUS_SERVER_ERROR.getStatus());
			Map<String, String> infoMap = new HashMap<String, String>();
			/* 报错信息的占位符 */
			infoMap.put("errinfo", e.toString());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), infoMap);
		}
		return apiResponse;
	}
}
